package cn.edu.zut.score.core.domain;

import io.swagger.annotations.ApiModel;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @author zby
 * @date 2021/06/24
 */
@ApiModel("分页请求封装")
public class PageRequest {
    public static final Integer DFT_PAGE_SIZE = 10;
    public static final Integer DEFAULT_PAGE_NUM = 1;
    @NotNull(
            message = "页码不能为空"
    )
    @Min(
            value = 1L,
            message = "页码应该从1开始"
    )
    private Integer page;
    private Integer limit;
    private String sortBy;
    private String order;

    public PageRequest() {
        this.page = DEFAULT_PAGE_NUM;
        this.limit = DFT_PAGE_SIZE;
    }

    private PageRequest(Integer page, Integer limit) {
        this.page = DEFAULT_PAGE_NUM;
        this.limit = DFT_PAGE_SIZE;
        this.page = page;
        this.limit = limit;
    }

    public Long getStartRow() {
        return (long) (this.page - 1) * this.limit;
    }

    public static PageRequest of(Integer page, Integer limit) {
        return new PageRequest(page, limit);
    }

    public Integer getPage() {
        return this.page;
    }

    public Integer getLimit() {
        return this.limit;
    }

    public String getSortBy() {
        return this.sortBy;
    }

    public String getOrder() {
        return this.order;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public void setSortBy(String sortBy) {
        this.sortBy = sortBy;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof PageRequest)) {
            return false;
        } else {
            PageRequest other = (PageRequest) o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label59:
                {
                    Object this$page = this.getPage();
                    Object other$page = other.getPage();
                    if (this$page == null) {
                        if (other$page == null) {
                            break label59;
                        }
                    } else if (this$page.equals(other$page)) {
                        break label59;
                    }

                    return false;
                }

                Object this$limit = this.getLimit();
                Object other$limit = other.getLimit();
                if (this$limit == null) {
                    if (other$limit != null) {
                        return false;
                    }
                } else if (!this$limit.equals(other$limit)) {
                    return false;
                }

                Object this$sortBy = this.getSortBy();
                Object other$sortBy = other.getSortBy();
                if (this$sortBy == null) {
                    if (other$sortBy != null) {
                        return false;
                    }
                } else if (!this$sortBy.equals(other$sortBy)) {
                    return false;
                }

                Object this$order = this.getOrder();
                Object other$order = other.getOrder();
                if (this$order == null) {
                    if (other$order != null) {
                        return false;
                    }
                } else if (!this$order.equals(other$order)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof PageRequest;
    }

    @Override
    public int hashCode() {
        int result = 1;
        Object $page = this.getPage();
        result = result * 59 + ($page == null ? 43 : $page.hashCode());
        Object $limit = this.getLimit();
        result = result * 59 + ($limit == null ? 43 : $limit.hashCode());
        Object $sortBy = this.getSortBy();
        result = result * 59 + ($sortBy == null ? 43 : $sortBy.hashCode());
        Object $order = this.getOrder();
        result = result * 59 + ($order == null ? 43 : $order.hashCode());
        return result;
    }

    @Override
    public String toString() {
        return "PageRequest(page=" + this.getPage() + ", limit=" + this.getLimit() + ", sortBy=" + this.getSortBy() + ", order=" + this.getOrder() + ")";
    }
}

